package com.nuanshui.heatedloan.web.appsystem;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.baseservice.constant.MediaBizType;
import com.nuanshui.heatedloan.baseservice.facade.AdvertisementService;
import com.nuanshui.heatedloan.baseservice.facade.ArticleService;
import com.nuanshui.heatedloan.baseservice.facade.MediaService;
import com.nuanshui.heatedloan.baseservice.form.ArticleForm;
import com.nuanshui.heatedloan.baseservice.model.ArticleType;
import com.nuanshui.heatedloan.baseservice.vo.ArticleVO;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.module.model.AdvertisementBizType;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.web.ApiController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: Gonzo
 * @Date 2017/5/11下午8:02
 * @since 1.4.2
 */
@RestController
@RequestMapping("/cms/manage/articlemanage")
public class ArticleManageController extends ApiController {

    @Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
    private ArticleService articleService;

    @RequestMapping(value = "/articleTypeList",method = RequestMethod.POST)
    public String articleTypeList(){
        Map<String, Object> result = Maps.newHashMap();
        ArticleType museumArticleType = new ArticleType();
        List<ArticleType> data =  articleService.getArticleType(museumArticleType);
        result.put("list", data);
        return success(result);
    }

    @RequestMapping(value = "/deleteArticleType",method = RequestMethod.POST)
    public String deleteArticleType(String id){
        Pager<ArticleVO> page = new Pager<ArticleVO>(0, 1);

        page = articleService.getArticleListByType(id, page);
        if (page.getData().size() > 0) {
            return fail("该分类下有文章, 无法删除...");
        }
        articleService.deleteArticleTypeById(id);
        return success();
    }

    @RequestMapping(value = "/editArticleType",method = RequestMethod.POST)
    public String editArticleType(ArticleType articleType){
        if (SysStringUtils.isEmpty(articleType.getId())) {
            articleService.saveArticleType(articleType);
        } else {
            articleService.updateArticleType(articleType);
        }
        return success();
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public String list(String query, Pager<ArticleVO> pager,String catId){
        ArticleForm articleForm = JSON.parseObject(query,ArticleForm.class);
        articleForm.setCatId(catId);
        pager = articleService.loadArticle(pager,articleForm);
        return success(pager.asHashMap());
    }

    @RequestMapping(value = "/saveArticle",method = RequestMethod.POST)
    public String createArticle(ArticleForm articleForm){
        if(SysStringUtils.isEmpty(articleForm.getId()))
        {
            articleService.save(articleForm);
        } else
        {
            articleService.update(articleForm);
        }

        return success(articleForm);
    }

    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public String updateStatus(String id, Integer status){
        articleService.changeStatus(status, id);
        return success();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String delete(String id){
        articleService.changeStatus(0, id);
        return success();
    }

    @RequestMapping("/detail")
    public String detail(String id){
        ArticleVO articleVO = articleService.loadVo(id);
        Map<String, Object> result = Maps.newHashMap();
        result.put("data", articleVO);
        return success(result);
    }

}
